﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class MP_JobSiteAreas : System.Web.UI.Page
    {
        private SsMng ssMng = new SsMng();
        private IMasAreaBLL areaBLL = new MasAreaBLL();
        private IJobSiteAreaBLL jobAreaBLL = new JobSiteAreaBLL();
        private ITreeViewBLL treeBLL = new TreeViewBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadRootNodes(RadTreeView1, TreeNodeExpandMode.ServerSideCallBack);
                RadTreeNode selected_node = RadTreeView1.SelectedNode;
            }

        }
        public void LoadDataGrid()
        {
            //IList<JobSiteAreaInfo> Info = jobAreaBLL.ListJobSiteAreas(Session["JobSiteSeq"].ToString());
            DataSet ds = jobAreaBLL.LoadJobSiteAreas((int?)null, Session["JobSiteSeq"].ToString());
            DataTable dt = null;

            if (ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];                
            }

            RadGrid1.DataSource = dt;
            RadGrid1.DataBind();
        }
         private void LoadRootNodes(RadTreeView treeView, TreeNodeExpandMode expandMode)
        {
            IList<TreeViewInfo> infoList = treeBLL.LoadJobSiteAreaTreeView("");

            foreach (TreeViewInfo info in treeBLL.LoadJobSiteAreaTreeView(""))
            {
                RadTreeNode node = new RadTreeNode();
                node.Text = info.Name;
                node.ToolTip = info.Descr;
                node.Value = info.Id;
                node.ImageUrl = info.Image_url;
                if (Convert.ToInt32(info.Children_count) > 0)
                {
                    node.ExpandMode = expandMode;
                }
                treeView.Nodes.Add(node);
            }
        }

        private void PopulateNodeOnDemand(RadTreeNodeEventArgs e, TreeNodeExpandMode expandMode)
        {
            e.Node.Dispose();
            e.Node.Nodes.Clear();

            foreach (TreeViewInfo info in treeBLL.LoadJobSiteAreaTreeView(e.Node.Value))
            {
                RadTreeNode node = new RadTreeNode();
                node.Text = info.Name;
                node.ToolTip = info.Descr;
                node.Value = info.Id;
                node.ImageUrl = info.Image_url;
                if (Convert.ToInt32(info.Children_count) > 0)
                {
                    node.ExpandMode = expandMode;
                }
                e.Node.Nodes.Add(node);
            }

            e.Node.Expanded = true;
            e.Node.ExpandMode = expandMode;
        }

        public void PopulateNodeOnEditGrid(RadTreeNodeEventArgs e, TreeNodeExpandMode expandMode)
        {
            foreach (TreeViewInfo info in treeBLL.LoadJobSiteAreaTreeView(e.Node.Value))
            {
                RadTreeNode node = new RadTreeNode();
                node.Text = info.Name;
                node.ToolTip = info.Descr;
                node.Value = info.Id;
                node.ImageUrl = info.Image_url;
                if (Convert.ToInt32(info.Children_count) > 0)
                {
                    node.ExpandMode = expandMode;
                }
                e.Node.Nodes.Add(node);
            }

            e.Node.Nodes.Clear();
            e.Node.ExpandMode = expandMode;
        }
        protected void cmdExpand_Click(object sender, ImageClickEventArgs e)
        {
            RadTreeNode node = new RadTreeNode();
            if (RadTreeView1.Nodes.Count > 0)
            {
                for (int i = 0; i < RadTreeView1.Nodes.Count; i++)
                {
                    node = RadTreeView1.Nodes[i];
                    node.ExpandChildNodes();
                }
                RadTreeView1.ExpandAllNodes();
            }
            //foreach (TreeViewInfo info in treeBLL.LoadJobSiteAreaTreeView(""))
            //{
            //    RadTreeNode node = new RadTreeNode();
            //    node.Text = info.Name;
            //    node.ToolTip = info.Descr;
            //    node.Value = info.Id;
            //    node.ImageUrl = info.Image_url;
            //    if (Convert.ToInt32(info.Children_count) > 0)
            //    {
            //        node.ExpandParentNodes();
            //    }
            //    //e. .Node.Nodes.Add(node);
            //}

            //RadTreeView1.ExpandAllNodes();
            //RadTreeView1.ExpandAllNodes();
            //IList<TreeViewInfo> infoList = treeBLL.LoadJobSiteAreaTreeView("");

            //RadTreeView1.Nodes.Clear();
            //RadTreeView1.DataSource = infoList;
            //RadTreeView1.DataBind();
            //RadTreeView1.ExpandAllNodes();
        }

        protected void cmdCollapse_Click(object sender, ImageClickEventArgs e)
        {
            RadTreeView1.CollapseAllNodes();
        }

        protected void RadTreeView1_NodeExpand(object sender, RadTreeNodeEventArgs e)
        {
            PopulateNodeOnDemand(e, TreeNodeExpandMode.ServerSideCallBack);

        }
        protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "Delete" )
            {
                int jobAreaSeq = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["JOB_SITE_AREA_SEQ"]);

                jobAreaBLL.Delete(jobAreaSeq, ssMng.EmpSeqI);
            }
            LoadDataGrid();
            RefreshTree();
        }
        public void RefreshTree()
        {
            RadTreeNode selected_node = RadTreeView1.SelectedNode;
            if (selected_node == null)
            {
                selected_node = RadTreeView1.FindNodeByValue("0");
            }
            RadTreeNodeEventArgs node = new RadTreeNodeEventArgs(selected_node);
            RadAjaxManager1.ResponseScripts.Add(@"CollapseSelectTree();");
            PopulateNodeOnEditGrid(node, TreeNodeExpandMode.ServerSideCallBack);
            RadAjaxManager1.ResponseScripts.Add(@"ExpandSelectTree();");
        }
        protected void RadTreeView1_NodeClick(object sender, RadTreeNodeEventArgs e)
        {
            RadTreeNode selected_node = RadTreeView1.SelectedNode;

            string value = selected_node.Value;
            string[] root_value = selected_node.Value.Split(new string[] { "-" }, StringSplitOptions.None);

            if (!string.IsNullOrEmpty(value))
            {                
                bool bNode3 = false;                

                if (selected_node.ParentNode != null)
                {
                    if (selected_node.Level == 3 || selected_node.ParentNode.Value == "1")
                    {
                        bNode3 = true;
                        Session["JobSiteSeq"] = root_value[1];
                        LoadDataGrid();
                    }
                }
                // ให้ show เฉพาะตอนที่เลือกระดับหน่วยงาน
                lblHeader.Visible = bNode3;
                lblUpperText.Visible = bNode3;
                RadGrid1.Visible = bNode3;
                lblWarning.Visible = !bNode3;
            }

        }

        protected void cmdSearch_Click(object sender, ImageClickEventArgs e)
        {


        }
    }
}
